package com.wxl.common.util;

import android.text.TextUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class StringUtils {

    /**
     * md5加密
     */
    public static String string2MD5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 将十六进制串转换为二进制
     */
    public static String HexToBin16(String hexStr) {
        int ten = Integer.parseInt(hexStr, 16);
        String binStr = Integer.toBinaryString(ten);
        if (binStr.length() < 16) {
            for (int i = binStr.length(); i < 16; i++) {
                binStr = "0" + binStr;
            }
        }
        return binStr;
    }

    public static String HexToBin162(String hexStr) {
        int ten = Integer.parseInt(hexStr, 16);
        String binStr = Integer.toBinaryString(ten);
        return binStr;
    }

    public static String TenTo16(String tenStr) {
        String binaryString = Integer.toBinaryString(Integer.parseInt(tenStr));
        return String.valueOf(Integer.valueOf(tenStr, 16));
    }

    public static String TenToRowHole16(String row, String hole) {
        String rowBinaryString = Integer.toBinaryString(Integer.parseInt(row)-1);
        String holeBinaryString = Integer.toBinaryString(Integer.parseInt(hole)-1);
        StringBuffer rs = new StringBuffer();
        if (rowBinaryString.length() < 6) {
            for (int i = 0; i < 6 - rowBinaryString.length(); i++) {
                rs.append("0");
            }
            rs.append(rowBinaryString);
        }
        StringBuffer hs = new StringBuffer();
        if (holeBinaryString.length() < 10) {
            for (int i = 0; i < 10 - holeBinaryString.length(); i++) {
                hs.append("0");
            }
            hs.append(holeBinaryString);
        }

        int r = Integer.parseInt(rs.toString() + hs.toString(), 2);

        String s = Integer.toHexString(r);

        return s;
    }

    /**
     * 将十六进制串转换为二进制
     */
    public static String HexToBin8(String hexStr) {
        int ten = Integer.parseInt(hexStr, 16);
        String binStr = Integer.toBinaryString(ten);
        if (binStr.length() < 8) {
            for (int i = binStr.length(); i < 8; i++) {
                binStr = "0" + binStr;
            }
        }
        return binStr;
    }


    /**
     * 将雷管UID进行处理
     */
    public static String getUid(String uid) {
        StringBuilder sb = new StringBuilder();
        if (!TextUtils.isEmpty(uid) && uid.length() > 15) {
            if (uid.length() < 4) {
                return uid;
            }
            String unit = uid.substring(0, 4);
            if (unit.endsWith("00"))
                sb.append(unit.substring(0, unit.length() - 2));
            sb.append(Integer.valueOf(uid.substring(5, 6), 16));
            int month = Integer.valueOf(uid.substring(6, 8), 16);
            if (month < 10)
                sb.append("0");
            sb.append(month);
            int day = Integer.valueOf(uid.substring(8, 10), 16);
            if (day < 10)
                sb.append("0");
            sb.append(day);
            String boxNum = uid.substring(10, 14);
//            if (boxNum < 10)
//                sb.append("000");
//            else if (boxNum < 100)
//                sb.append("00");
//            else if (boxNum < 1000)
//                sb.append("0");
            sb.append(boxNum);
            String serialNum = uid.substring(14, 16);
//            if (serialNum < 10)
//                sb.append("0");
            sb.append(serialNum);
        }
        return sb.toString();
    }

    /**
     * 将雷管输入的UID进行处理
     */
    public static String getUid2(String uid) {
        StringBuilder sb = new StringBuilder();
        if (!TextUtils.isEmpty(uid)) {
            String str = "";
            if (uid.length() == 13) {
                sb.append(uid.substring(0, 2) + "000");
                str = uid.substring(2, uid.length());
            } else if (uid.length() == 15) {
                sb.append(uid.substring(0, 4) + 0);
                str = uid.substring(4, uid.length());
            }
            int year = Integer.parseInt(str.substring(0, 1));
            String sixYear = Integer.toHexString(year);
            sb.append(sixYear);
            int month = Integer.parseInt(str.substring(1, 3));
            String sixMonth = Integer.toHexString(month);
            if (sixMonth.length() == 1)
                sb.append("0");
            sb.append(sixMonth);
            int day = Integer.parseInt(str.substring(3, 5));
            String sixDay = Integer.toHexString(day);
            if (sixDay.length() == 1)
                sb.append("0");
            sb.append(sixDay);
            sb.append(str.substring(5, str.length()));
        }
        return sb.toString();
    }


    public static int getRowNumber(String hexString) {
        return Integer.parseInt(hexString.substring(0, 6), 2) + 1;
    }

    public static int getHoleNumber(String hexString) {
        return Integer.parseInt(hexString.substring(6, 16), 2) + 1;
    }

    /**
     * 将version换成缩小100倍的字符串
     */
    public static String getPointVersion(String version) {
        if (version.length() == 1)
            version = "0.0" + version;
        else if (version.length() == 2)
            version = "0." + version;
        else {
            version = version.substring(0, version.length() - 2) + "." + version.substring(version.length() - 2, version.length());
        }
        return version;
    }
}
